package com.bishe.demo.modules.hotel.hotelPolicy.service.impl;

import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.hotel.hotelPolicy.dao.*;
import com.bishe.demo.modules.hotel.hotelPolicy.entity.*;
import com.bishe.demo.modules.hotel.hotelPolicy.service.PolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class PolicyServiceImpl implements PolicyService {
    @Autowired
    private PolicyDao policyDao;

    @Autowired
    private CheckDao checkDao;

    @Autowired
    private PolicyCheckDao policyCheckDao;

    @Autowired
    private CheckTypeDao checkTypeDao;

    @Autowired
    private PolicyCheckTypeDao policyCheckTypeDao;

    @Autowired
    private AddBedDao addBedDao;

    @Autowired
    private PolicyAddBedDao policyAddBedDao;

    @Autowired
    private BreakfastDao breakfastDao;

    @Autowired
    private PolicyBreakfastDao policyBreakfastDao;

    @Autowired
    private ParkingDao parkingDao;

    @Autowired
    private PolicyParkingDao policyParkingDao;

    @Autowired
    private BreakfastTypeDao breakfastTypeDao;

    @Autowired
    private BreakfastBreakfastTypeDao breakfastBreakfastTypeDao;

    @Autowired
    private MenuTypeDao menuTypeDao;

    @Autowired
    private BreakfastMenuTypeDao breakfastMenuTypeDao;

    @Autowired
    private PolicyUserDao policyUserDao;

    @Override
    @Transactional
    public ResultEntity<Policy> insertPolicy(Policy policy) {
        policy.setPolicyId(policy.getUser().getUserId());

        Policy policyByUserId = policyDao.getPolicyByUserId(policy.getUser().getUserId());
        if (policyByUserId == null) {

            policyDao.insertPolicy(policy);

            Check check = policy.getCheck();
            checkDao.insertCheck(check);
            policyCheckDao.insertPolicyCheck(new PolicyCheck(policy.getPolicyId(), check.getCheckId()));

            CheckType checkType = policy.getCheckType();
            checkTypeDao.insertCheckType(checkType);
            policyCheckTypeDao.insertPolicyCheckType(new PolicyCheckType(policy.getPolicyId(), checkType.getCheckTypeId()));

            AddBed addBed = policy.getAddBed();
            addBedDao.insertAddBed(addBed);
            policyAddBedDao.insertPolicyAddBed(new PolicyAddBed(policy.getPolicyId(), addBed.getAddBedId()));

            Breakfast breakfast = policy.getBreakfast();
            breakfastDao.insertBreakfast(breakfast);
            List<BreakfastType> breakfastTypes = breakfast.getBreakfastTypes();
            for (BreakfastType breakfastType : breakfastTypes) {
                breakfastTypeDao.insertBreakfastType(breakfastType);
                breakfastBreakfastTypeDao.insertBreakfastBreakfastType(new BreakfastBreakfastType(breakfast.getBreakfastId(), breakfastType.getBreakfastTypeId()));
            }
            List<MenuType> menuTypes = breakfast.getMenuTypes();
            for (MenuType menuType : menuTypes) {
                menuTypeDao.insertMenuType(menuType);
                breakfastMenuTypeDao.insertBreakfastMenuType(new BreakfastMenuType(breakfast.getBreakfastId(), menuType.getMenuTypeId()));
            }
            policyBreakfastDao.insertPolicyBreakfast(new PolicyBreakfast(policy.getPolicyId(), breakfast.getBreakfastId()));

            Parking parking = policy.getParking();
            parkingDao.insertParking(parking);
            policyParkingDao.insertPolicyParking(new PolicyParking(policy.getPolicyId(), parking.getParkingId()));

            policyUserDao.insertPolicyUser(new PolicyUser(policy.getPolicyId(), policy.getUser().getUserId()));

            return new ResultEntity<Policy>(ResultEntity.ResultStatus.SUCCESS.code, "添加成功", policy);
        } else {
            return new ResultEntity<Policy>(ResultEntity.ResultStatus.FAILED.code, "已有酒店政策");
        }
    }

    @Override
    public Policy getPolicyByHotelId(int hotelId) {
        return policyDao.getPolicyByHotelId(hotelId);
    }

    @Override
    public void updatePolicy(Policy policy) {
        deletePolicy(policy);
    }

    @Override
    public void deletePolicy(Policy policy) {
        Check checkByPolicyId = checkDao.getCheckByPolicyId(policy.getPolicyId());
        checkDao.deleteCheckByCheckId(checkByPolicyId.getCheckId());
        policyCheckDao.deleteByPolicyId(policy.getPolicyId());

        CheckType checkTypeByPolicyId = checkTypeDao.getCheckTypeByPolicyId(policy.getPolicyId());
        checkTypeDao.deleteByCheckTypeId(checkTypeByPolicyId.getCheckTypeId());
        policyCheckTypeDao.deleteByPolicyId(policy.getPolicyId());
        policyDao.deletePolicyByPolicyId(policy.getPolicyId());

        AddBed addBedByPolicyId = addBedDao.getAddBedByPolicyId(policy.getPolicyId());
        addBedDao.deleteByAddBedId(addBedByPolicyId.getAddBedId());
        policyAddBedDao.deleteByPolicyId(policy.getPolicyId());

        Breakfast breakfastByPolicyId = breakfastDao.getBreakfastByPolicyId(policy.getPolicyId());
        List<BreakfastType> breakfastTypes = breakfastByPolicyId.getBreakfastTypes();
        for (BreakfastType breakfastType : breakfastTypes) {
            breakfastTypeDao.deleteById(breakfastType.getBreakfastTypeId());
            breakfastBreakfastTypeDao.deleteByBreakfastId(breakfastByPolicyId.getBreakfastId());
        }
        List<MenuType> menuTypes = breakfastByPolicyId.getMenuTypes();
        for (MenuType menuType : menuTypes) {
            menuTypeDao.deleteByMenuTypeId(menuType.getMenuTypeId());
            breakfastMenuTypeDao.deleteByBreakfastId(breakfastByPolicyId.getBreakfastId());
        }
        breakfastDao.deleteByBreakfastId(breakfastByPolicyId.getBreakfastId());
        policyBreakfastDao.deleteByPolicyId(policy.getPolicyId());

        Parking parkingByPolicyId = parkingDao.getParkingByPolicyId(policy.getPolicyId());
        parkingDao.deleteByParkingId(parkingByPolicyId.getParkingId());
        policyParkingDao.deleteByPolicyId(policy.getPolicyId());

        policyUserDao.deleteByPolicyId(policy.getPolicyId());
    }
}
